
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.AdvertDao;
import com.sun.showcase.biz.mall.service.AdvertService;
import com.sun.showcase.client.domain.mall.Advert;
import com.sun.showcase.client.query.mall.AdvertQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.UuidUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("advertService")
@Transactional
public class AdvertServiceImpl implements AdvertService{
	@Autowired
	private AdvertDao advertDao;
	
	public void setAdvertDao(AdvertDao dao) {
		this.advertDao = dao;
	}

	public DataGrid datagrid(AdvertQuery advertQuery) {
		DataGrid j = new DataGrid();
		Pager<Advert> pager  = find(advertQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<AdvertQuery> getQuerysFromEntitys(List<Advert> adverts) {
		List<AdvertQuery> advertQuerys = new ArrayList<AdvertQuery>();
		if (adverts != null && adverts.size() > 0) {
			for (Advert tb : adverts) {
				AdvertQuery b = new AdvertQuery();
				BeanUtils.copyProperties(tb, b);
				advertQuerys.add(b);
			}
		}
		return advertQuerys;
	}

	private Pager<Advert> find(AdvertQuery advertQuery) {
		Page<Advert> page = PageHelper.startPage(advertQuery.getPage().intValue(), advertQuery.getRows().intValue());
		List<Advert> list = advertDao.findList(advertQuery);
		Pager<Advert> pager = new Pager<Advert>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Advert add(AdvertQuery advertQuery) {
		Advert t = new Advert();
		BeanUtils.copyProperties(advertQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		advertDao.save(t);
		advertQuery.setId(t.getId());
		return t;
		
	}

	public void update(AdvertQuery advertQuery) {
		Advert t = advertDao.getById(advertQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(advertQuery, t);
	    	advertDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(AdvertQuery advertQuery) {
	    advertDao.updatePart(advertQuery);
	}

	public void delete(String[] ids) {
		advertDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		advertDao.deletePt(ids);
	}

	public Advert get(AdvertQuery advertQuery) {
		return advertDao.getById(advertQuery.getId());
	}

	public Advert get(String id) {
		return advertDao.getById(id);
	}

	
	public List<AdvertQuery> listAll(AdvertQuery advertQuery) {
	    List<Advert> list = advertDao.findList(advertQuery);
		List<AdvertQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
